package com.doyoo.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.doyoo.common.ResponseResult;
import com.doyoo.entity.JobStepTemplate;
import com.doyoo.enums.Type;
import com.doyoo.pojo.AddJobTemplateDto;
import com.doyoo.service.JobStepTemplateService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.EnumUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 定时任务-步骤模板 前端控制器
 */
@Tag(name = "任务步骤模板(JobStepTemplate)")
@RestController
@RequestMapping("/job-step-templates")
public class JobStepTemplateController {

    @Autowired
    private JobStepTemplateService jobStepTemplateService;

    @Operation(summary = "任务步骤模板-列表")
    @GetMapping
    public ResponseResult<List<JobStepTemplate>> listAll(@RequestParam(required = false) String type) {
        return ResponseResult.success(jobStepTemplateService.listAll(type));
    }

    @Operation(summary = "任务步骤模板-新增单个")
    @PostMapping
    public ResponseResult addOne(@RequestBody AddJobTemplateDto dto) {
        JobStepTemplate jobStepTemplate = new JobStepTemplate();
        BeanUtils.copyProperties(dto, jobStepTemplate);
        jobStepTemplateService.save(jobStepTemplate);
        return ResponseResult.success();
    }

    @Operation(summary = "任务步骤模板-查看单个")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "模板id", dataTypeClass = Integer.class)})
    @GetMapping("/{id}")
    public ResponseResult<JobStepTemplate> getOne(@PathVariable Integer id) {
        return ResponseResult.success(jobStepTemplateService.getById(id));
    }

    @Operation(summary = "任务步骤模板-更新单个")
    @PutMapping("/{id}")
    public ResponseResult updateOne(@PathVariable Integer id, @RequestBody JobStepTemplate jobStepTemplate) {
        jobStepTemplate.setId(id);
        jobStepTemplateService.saveOrUpdate(jobStepTemplate);
        return ResponseResult.success();
    }

    @Operation(summary = "任务步骤模板-删除单个")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "模板id", dataTypeClass = Integer.class)})
    @DeleteMapping("/{id}")
    public ResponseResult deleteOne(@PathVariable Integer id) {
        jobStepTemplateService.removeById(id);
        return ResponseResult.success();
    }

    @Operation(summary = "任务步骤模板-根据参数生成命令")
    @GetMapping("/{id}/codes")
    public ResponseResult<String> getCodes(@PathVariable Integer id, @RequestParam String templateParameter) throws JsonProcessingException {
        return ResponseResult.success(jobStepTemplateService.getCodes(id, templateParameter));
    }
}
